package com.xugongzhang.springcloud.controller;

import com.xugongzhang.springcloud.entities.CommonResult;
import com.xugongzhang.springcloud.entities.Payment;
import com.xugongzhang.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @AUTHOR X
 * @DATE 2022/5/17 23:36
 */
@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping(value = "/payment/insert")
    public CommonResult create(@RequestBody Payment payment) {

        int result = paymentService.addPayment(payment);
        log.info("插入结果：" + result);

        if (result > 0) {
            return new CommonResult(200, "插入数据库成功,port:" + serverPort, result);
        }
        else {
            return new CommonResult(500, "插入数据库失败", null);
        }
    }

    @GetMapping(value = "/payment/get/{id}")
    public CommonResult select(@PathVariable("id") Long id) {

        Payment result = paymentService.getPaymentById(id);
        log.info("查询结果：" + result);

        if (null != result) {
            return new CommonResult(200, "查到了,port:" + serverPort, result);
        }
        else {
            return new CommonResult(500, "没查到", null);
        }
    }
}
